using System;
using SoftwareMonkeys.LiveShift.Business;
using System.Collections.Generic;
using SoftwareMonkeys.LiveShift.Diagnostics;
using NLog;

namespace SoftwareMonkeys.LiveShift.Entities
{
	/// <summary>
	/// Description of LiveEntity.
	/// </summary>
	[Serializable]
	public class ShiftableEntity : BaseEntity, IShiftableEntity
	{
		/*private IShifter shifter;
        public virtual IShifter Shifter
        {
            get { return shifter; }
            set { shifter = value; }
        }*/

		private IBehavior[] behaviors;

		public IBehavior[] Behaviors
		{
			get {
				if (behaviors == null)
					behaviors = new IBehavior[] { };
				return behaviors; }
			set { behaviors = value; }
		}

		private DateTime lastShift;
		public DateTime LastShift
		{
			get { return lastShift; }
			set { lastShift = value; }
		}

		/*private LivePropertyCollection properties;
		public LivePropertyCollection Properties
		{
			get {
				if (properties == null)
					properties = new LivePropertyCollection();
				return properties; }
			set { properties = value; }
		}*/
		
		private IShiftEnvironment environment;
		public IShiftEnvironment Environment
		{
			get { return environment; }
			set { environment = value; }
		}
		
		public ShiftableEntity()
		{
			LastShift = DateTime.Now;
		}
		
		/*public void Shift(DateTime dateTime)
		{
            foreach (LiveProperty property in Properties)
            {
                IShifter shifter =
            }
		}*/
		
		public virtual void Initialize()
		{
			throw new NotImplementedException("Method must be overridden.");
		}

		public virtual IShiftResult Shift(DateTime now)
		{
			IShiftResult result = new ShiftResult();
			
			using (LogGroup logGroup = LogGroup.Start("Shifting the entity.", NLog.LogLevel.Debug))
			{
				if (Behaviors != null)
				{
					foreach (IBehavior behavior in Behaviors)
					{
						result.AddResult(behavior.Shift(now));
					}
				}
				else
					LogWriter.Debug("No behaviors defined.");
				
				LogWriter.Debug("New last shift date: " + now.ToString());
				
				LastShift = now;
			}

			return result;
		}

		public void AddBehavior(IBehavior behavior)
		{
			using (LogGroup logGroup = LogGroup.Start("Adding the provided behavior to the entity.", NLog.LogLevel.Debug))
			{
				List<IBehavior> behaviors = new List<IBehavior>(Behaviors);
				behaviors.Add(behavior);
				Behaviors = behaviors.ToArray();
			}

		}
	}
}
